﻿using BLL;
using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;


namespace Student.业务界面
{
    public partial class 选择导师 : Form
    {
        // 添加变量
        StuChoice stuChoice = new StuChoice();
        List<Teacher> teachers;
        B_Teacher b_Teacher = new B_Teacher();

        public 选择导师()
        {
            InitializeComponent();
        }

        // 提交_Click 完成
        private void button1_Click(object sender, EventArgs e)//提交选择
        {
            //判断超时
            if (Models.LogInfo.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            stuChoice.Group_id = (int)LogInfo.student.group_id;
            BLL.B_StuChoice b_G = new BLL.B_StuChoice();

            try
            {
                if (b_G.commit(stuChoice))
                    MessageBox.Show("ok!");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            teachers = b_Teacher.GetList();
            dataGridView1.DataSource = teachers;
        }

        // selected_box 完成, 一个帮助函数, 多选框
        private System.Windows.Forms.TextBox selected_box(out int choice)
        {
            System.Windows.Forms.TextBox selected;

            if (radioButton1.Checked)
            {
                selected = textBox3; 
                choice = 1; 
            }
            else if (radioButton2.Checked)
            { 
                selected = textBox4; 
                choice = 2; 
            }
            else
            {
                selected = textBox5;
                choice = 3; 
            }

            return selected;
        }

        // teacher_list_MouseClick完成
        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            int idx = dataGridView1.CurrentCell.RowIndex;
            int choice;
            TextBox selected = selected_box(out choice);
            selected.Text = teachers[idx].teacher_name;
            textBox1.Text = teachers[idx].teacher_name;
            textBox2.Text = teachers[idx].info;

            if (choice == 1)
                stuChoice.Choice_1 = teachers[idx].teacher_id;
            else if (choice == 2)
                stuChoice.Choice_2 = teachers[idx].teacher_id;
            else
                stuChoice.Choice_3 = teachers[idx].teacher_id;
        }

      
        private void 选择导师_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.Hide();
            //学生端主界面 f = new 学生端主界面();
            //f.ShowDialog();
        }

        // 对应select_teacher_Load 完成
        private void 选择导师_Load_1(object sender, EventArgs e)
        {
            teachers = b_Teacher.GetList();


            dataGridView1.DataSource = teachers;
            B_StuChoice b_stuChoice = new B_StuChoice();
            stuChoice = b_stuChoice.Select(LogInfo.student);

            //展示文本框
            if (stuChoice.Choice_1 != null)
                textBox3.Text = teachers.Where(t => t.teacher_id == stuChoice.Choice_1).First().teacher_name;
            if (stuChoice.Choice_2 != null)
                textBox4.Text = teachers.Where(t => t.teacher_id == stuChoice.Choice_2).First().teacher_name;
            if (stuChoice.Choice_3 != null)
                textBox5.Text = teachers.Where(t => t.teacher_id == stuChoice.Choice_3).First().teacher_name;
        }
    }
}
